/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-05-26
 * Time: 16:16
 */
public class MaxProfit {
    public int maxProfit(int[] prices) {
        int ret = 0; // 记录最终结果
        for (int i = 0, prevMin = Integer.MAX_VALUE; i < prices.length; i++) {
            ret = Math.max(ret, prices[i] - prevMin); // 先更新结果
            prevMin = Math.min(prevMin, prices[i]); // 再更新最⼩值
        }
        return ret;
    }
}
